import {Component, Input, OnInit} from '@angular/core';
import {Monster} from '../lunhui-web-front-dto/Monster';
import {BrowserUtils} from '../lunhui-web-front-utils/BrowserUtils';

@Component({
    selector: 'app-lunhui-web-front-map-monster',
    templateUrl: './lunhui-web-front-map-monster.component.html',
    styleUrls: ['./lunhui-web-front-map-monster.component.scss']
})
export class LunhuiWebFrontMapMonsterComponent implements OnInit {

    @Input() monsterMapInfo: Array<Monster>;

    public monsterInfoMap: any;
    public objectKeys: any;

    constructor() {
        this.monsterInfoMap = {};
        this.objectKeys = Object.keys;
    }

    ngOnInit() {
        const result: boolean = BrowserUtils.checkToken();

        if (!result) {
            BrowserUtils.backToTarget('login');
            return;
        }

        this.handleMonsterMapInfo(this.monsterMapInfo);
    }

    private handleMonsterMapInfo(monsterMapInfo: Array<Monster>): void {

        const _this: LunhuiWebFrontMapMonsterComponent = this;
        let key: string;

        monsterMapInfo.forEach(monster => {
            key = `(${monster.positionX},${monster.positionY})`;

            if (!_this.monsterInfoMap.hasOwnProperty(key)) {
                const name: string = monster.monsterName + '.';
                monster.monsterName = name;

                _this.monsterInfoMap[key] = monster;
            } else {
                const entity: Monster = _this.monsterInfoMap[key];
                entity.monsterName = entity.monsterName + monster.monsterName + '.';

                _this.monsterInfoMap[key] = entity;
            }

        });
    }

}
